package com.rambo.sdh.sys.controller;

import com.rambo.sdh.common.controller.BaseController;
import com.rambo.sdh.sys.dao.inter.po.UserPO;
import com.rambo.sdh.sys.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        Assert.notNull(userService, "userService is required!");
        this.userService = userService;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(UserPO user) {
        user.setYxbz("Y");

        for (int i = 0; i < 5; i++) {
            UserPO user1 = userService.addUser(user);
            log.info("添加用户成功，用户 UUID{}:", user1.getUuid());

            UserPO user2 = userService.findUserByUuid(user1.getUuid());
            log.info("查找用户成功，用户 UUID{}:", user2.getUuid());

           /* user2.setName("edit" + user2.getName());
            UserPO user3 = userService.updateUser(user2);
            log.info("更新用户成功，用户名称：{}", user3.getName());

            UserPO user4 = userService.deleteUserById(user3.getUuid());
            log.info("删除用户成功，用户名称：{}", user4.getName());*/
        }
        return "common/default";
    }

    @RequestMapping("/get/users")
    public String getUserList(Model model) {
        UserPO userPO = new UserPO();
        userPO.setPasswd("ICy5YqxZB1uWSwcVLSNLcA ==");
        List<UserPO> userList = userService.getUserList(userPO);
        for (UserPO po : userList) {
            log.info("po" + po.toString());
        }
        return "common/default";
    }

    @RequestMapping("/ehcache")
    public String ehcacheTest() {
        test();
        return "common/default";
    }


    @RequestMapping("/redis")
    public String redisTest() {
        test();
        return "common/default";
    }

    private void test() {
        UserPO userPO = new UserPO();
        userPO.setPasswd("ICy5YqxZB1uWSwcVLSNLcA ==");
        userPO.setName("rambo");
        userPO.setSex("男");
        userPO.setYxbz("Y");

        UserPO userPO1 = userService.addUser(userPO);
        log.info("1.插入用户 userPO1 并放入缓存:" + userPO1.toString());

        UserPO userByUuid = userService.findUserByUuid(userPO1.getUuid());
        log.info("2. 使用 uuid 查询缓存中该用户：" + userByUuid.toString());

        userByUuid.setSex("女");
        UserPO userPO2 = userService.updateUser(userByUuid);
        log.info("3. 更新性别之后进行查询缓存中该用户：" +  userService.findUserByUuid(userPO2.getUuid()).toString());

        UserPO userPO3 = userService.deleteUserById(userPO2.getUuid());
        log.info("4. 删除数据库中该用户后，查看缓存中该用户：" + userService.findUserByUuid(userPO3.getUuid()));
    }
}